<?php
/**
 * @File	  : Msmarty.php
 * @Project	  : vncphathoc
 * @author	  : ThanhMinh
 * @email	  : thanhminhld@yahoo.com - thanhminhld@gmail.com
 * @copyright : 2009
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (file_exists(BASEPATH . "libraries/libs/Smarty.class.php")) {
	require_once(BASEPATH . "libraries/libs/Smarty.class.php");
} else {
	exit('File not found !');
}

if (file_exists(APPPATH . "libraries/KNG_ngLanguage.php")) {
	require_once(APPPATH . "libraries/KNG_ngLanguage.php");
} else {
	exit('File not found !');
}

class Msmarty extends Smarty
{
	private $language;
	/*
	|==========================================================
	| Constructor
	|==========================================================
	*/
	public function __construct()
	{
		parent::Smarty();
		$config = &get_config();
        $session = &load_class('Session');
		$this->compile_dir = APPPATH . "templates_c";
		$this->template_dir = APPPATH . "templates";

		$this->assign('base_url', $config['base_url']);
		$this->assign('base_media', $config['base_url'] . $config['tpl_media']);
		$this->assign('base_ckfinder', $config['base_url'] . $config['tpl_media'] . '/ckeditor/ckfinder/');

		if ($session->userdata('lang_tag')) {
		    $locale = $session->userdata('lang_tag');
		} else {
		    $locale = 'vn';
		    $session->set_userdata(array('lang_tag' => 'vn'));
		}
		$langPath = APPPATH . 'language';
		$this->language = new KNG_ngLanguage($locale, $langPath); // create a new language object
		$GLOBALS['_NG_LANGUAGE_'] = &$this->language;
        $this->register_prefilter("smarty_prefilter_i18n");

		log_message('debug', "Smarty Class Initialized");
	}
	/**
	 * fetch
	 *
	 * @param $_smarty_tpl_file
	 * @param $_smarty_cache_id
	 * @param $_smarty_compile_id
	 * @param $_smarty_display
	 */
	function fetch($_smarty_tpl_file, $_smarty_cache_id = null, $_smarty_compile_id = null, $_smarty_display = false) {
		// We need to set the cache id and the compile id so a new script will be
		// compiled for each language. This makes things really fast ;-)
		$_smarty_compile_id = $this->language->getCurrentLanguage().'-'.$_smarty_compile_id;
		$_smarty_cache_id = $_smarty_compile_id;

		// Now call parent method
		return parent::fetch( $_smarty_tpl_file, $_smarty_cache_id, $_smarty_compile_id, $_smarty_display );
    }
	/**
	* test to see if valid cache exists for this template
	*
	* @param string $tpl_file name of template file
	* @param string $cache_id
	* @param string $compile_id
	* @return string|false results of {@link _read_cache_file()}
	*/
	function is_cached($tpl_file, $cache_id = null, $compile_id = null)
	{
        if (!$this->caching) {
        	return false;
        }

        if (!isset($compile_id)) {
             $compile_id = $this->language->getCurrentLanguage().'-'.$this->compile_id;
             $cache_id = $compile_id;
        }

        return parent::is_cached($tpl_file, $cache_id, $compile_id);
	}
	/**
	 * view show template with resouce file
	 * @access public
	 * @param $resourceName
	 * @param array $params
	 *
	 */
	public function view( $resourceName, $params = array() )
	{
		if (empty($resourceName)) {
			show_error('Template resource is empty !');
		}
		if (strrpos($resourceName, '.') === false || (strrpos($resourceName, '.')+1) < strlen($resourceName)) {
			$resourceName .= '.tpl';
		}
		if ((strrpos($resourceName, '.')+1) == strlen($resourceName)) {
			show_error('Template : [$resourceName] unknow the extending !');
		}
		if (!file_exists($this->template_dir . '/' . $resourceName)) {
			show_error('Template : [$resourceName] not found !');
		}
		if (is_array($params) && count($params)) {
			foreach($params as $key=>$value) {
				$this->assign($key, $value);
			}
		}

		$this->display('header.tpl');
		$this->display($resourceName);
		$this->display('footer.tpl');
	}
}
/**
 * smarty_prefilter_i18n()
 * This function takes the language file, and rips it into the template
 * $GLOBALS['_NG_LANGUAGE_'] is not unset anymore
 *
 * @param $tpl_source
 * @return
 **/
function smarty_prefilter_i18n($tpl_source, &$smarty)
{
    if (!is_object($GLOBALS['_NG_LANGUAGE_'])) {
        die("Error loading Multilanguage Support");
    }
    // load translations (if needed)
    $GLOBALS['_NG_LANGUAGE_']->loadCurrentTranslationTable();
    // Now replace the matched language strings with the entry in the file
    return preg_replace_callback('/##(.+?)##/', '_compile_lang', $tpl_source);
}

/**
 * _compile_lang
 * Called by smarty_prefilter_i18n function it processes every language
 * identifier, and inserts the language string in its place.
 *
 */
function _compile_lang($key)
{
    return $GLOBALS['_NG_LANGUAGE_']->getTranslation($key[1]);
}
?>